package behaviour.creator;

import agent.BuildingAgent;
import agent.CreatorAgent;
import jade.core.behaviours.OneShotBehaviour;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;

public class AddAgentBehaviour extends OneShotBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2172021320182853689L;

	private double x=0, y=0, w=2, h=3;
	private CreatorAgent agent;

	public AddAgentBehaviour(CreatorAgent agent) {
		this.agent = agent;
	}

	@Override
	public void action() {
		if(agent.shallCreate() && ! (agent.isStopped())) {

			double minDim,maxDim;
			/*
			 * Gives the buildings' range of dimensions
			 * For now, we consider that height and width are in the same range
			 * But can be modified later...
			 */
			//minDim = 0.01;
			//maxDim = 0.06;
			minDim = 0.08;
			maxDim = 0.2;

			w = Math.random()*(maxDim-minDim) + minDim;
			h = Math.random()*(maxDim-minDim) + minDim;
			x = Math.min(Math.random(), 1-w);
			y = Math.min(Math.random(), 1-h);

			addBuilding(x,y,w,h);
		}
	}



	/**
	 * Ajouter un batiment sur la carte
	 */
	private void addBuilding(double a, double b, double w, double h) {
		String name = "building" + agent.getBuildingCounter();
		agent.incrementBuildingCounter();

		Object[] arguments = {a, b, w, h};
		AgentController building;
		
		try {
			building = agent.getMc().createNewAgent(name, BuildingAgent.class.getName(), arguments);
			building.start();
			
		} catch (StaleProxyException e) {
			e.printStackTrace();
		}		
	}

}
